package org.aaron.demos;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class ExampleAppWidgetProvider extends AppWidgetProvider {
	private static final String APP_ACTION = "org.aaron.demos.action.APPWIDGET_UPDATE";

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		Log.d("ExampleAppWidgetProvider", "onUpdate");
		Intent intent = new Intent();
		intent.setAction(APP_ACTION);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidgetlayout);
		remoteViews.setOnClickPendingIntent(R.id.myButton, pendingIntent);
		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
	}

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		Log.d("ExampleAppWidgetProvider", "onEnabled");
	}

	@Override
	public void onReceive(Context context, Intent intent) {

		if (APP_ACTION.equals(intent.getAction())) {
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.appwidgetlayout);
			remoteViews.setTextViewText(R.id.appwidgetTextView, "receive broadcast.");
			ComponentName componentName = new ComponentName(context, ExampleAppWidgetProvider.class);
			appWidgetManager.updateAppWidget(componentName, remoteViews);

		} else {
			super.onReceive(context, intent);
		}
		Log.d("ExampleAppWidgetProvider", "onReceive");
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
		Log.d("ExampleAppWidgetProvider", "onDeleted");
	}
}
